using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// A class to build a dynamic tab strip control from your web.sitemap file using the subdirectories
/// specified in the sitemap to create the tabs. If your sitemap contains localization information
/// the control will change the name of the tab to the users browser culture information if there
/// is a matching localization file
/// </summary>
public partial class TabStripControl : System.Web.UI.UserControl
{
    private int leftGutterWidth;

    private string activeTabFill        = "~/App_Themes/default/images/tabon_bg.gif";
	private string inactiveTabFill = "~/App_Themes/default/images/taboff_bg.gif";

	private string tabOnHome = "~/App_Themes/default/images/tabon_home.gif";
	private string tabOffHome = "~/App_Themes/default/images/taboff_home.gif";

	private string seperatorActive = "~/App_Themes/default/images/tabon_between.gif";
	private string seperatorInActive = "~/App_Themes/default/images/taboff_between.gif";

	private string lastTabActiveLeft = "~/App_Themes/default/images/tabon_left.gif";
	private string lastTabActiveRight = "~/App_Themes/default/images/tabon_right.gif";
	private string lastTabInactiveRight = "~/App_Themes/default/images/taboff_right.gif";

    private ArrayList nodes = new ArrayList();

    private int activeNode = 1;


    protected void Page_Load(object sender, EventArgs e)
    {
        SiteMapNode root = SiteMap.RootNode;
		


        if (root != null)
        {
            DataSet ds = new DataSet();
            string currentUrl = Request.Url.ToString().ToLower();

			//List<SiteMapNode> nodes = new List<SiteMapNode>();

            ds.Tables.Add();
            ds.Tables[0].Columns.Add("Index");
            ds.Tables[0].Columns.Add("Title");
            ds.Tables[0].Columns.Add("Url");
            ds.Tables[0].Columns.Add("PrevActive");
            ds.Tables[0].Columns.Add("TotalIndexes");

            int i = 0;
            bool prevActive = false;
			bool userHasAccess = false;
            foreach (SiteMapNode child in root.ChildNodes)
            {
                if (child.ChildNodes.Count > 0 && child["showAsTab"].ToLower() == "true")
                {
					IList roles = child.Roles;
					if (roles.Count > 0)
					{
						for (int j = 0; j < roles.Count; j++)
						{
							userHasAccess = Roles.IsUserInRole(roles[j].ToString());
							if (userHasAccess)
								break;
						}
					}
					else
					{
						// no roles
						userHasAccess = true;
					}
					if (userHasAccess)
					{
						ds.Tables[0].Rows.Add(
							new Object[] { i.ToString(), child.Title, child.Url, prevActive, root.ChildNodes.Count });
					
						
						if (currentUrl.Contains(ScrubNodeUrl(child.Url)))
						{
							prevActive = true;
						}
						else
						{
							prevActive = false;
						}
					}
                    i++;
                }
            }

            rTabItems.DataSource = ds;
            rTabItems.DataBind();

        }


    }

    private string ScrubNodeUrl(string url)
    {
        string output = string.Empty;

        output = url.ToLower();
        output = output.Replace("~", "");
        output = output.Replace("default.aspx", "");

        return output;
    }

    protected string GetStartingImage(object item)
    {
        DataRowView row = (DataRowView)item;
        string currentUrl = Request.Url.ToString().ToLower();
        int index = Convert.ToInt32(row["Index"]);
        int totalIndexes = Convert.ToInt32(row["TotalIndexes"]);
        bool prevActive = Convert.ToBoolean(row["PrevActive"]);
        bool isActive = currentUrl.Contains(ScrubNodeUrl(row["Url"].ToString()));


        if (index == 0 && isActive)            
        {
            return tabOnHome;
        }
        else if (index == 0 && !isActive)
        {
            return tabOffHome;
        }
        else if (isActive)
        {
            return lastTabActiveLeft;
        }
        else if (!isActive && prevActive)
        {
            return seperatorActive;
        }
        else if (!isActive && !prevActive)
        {
            return seperatorInActive;
        }
        else
        {
            return "~/unknown.jpg";
        }
    }

    protected string GetClassName(object item)
    {
        DataRowView row = (DataRowView)item;
        string currentUrl = Request.Url.ToString().ToLower();
        bool isActive = currentUrl.Contains(ScrubNodeUrl(row["Url"].ToString()));


        if (isActive)
        {
            return "active";
        }
        else
        {
            return "inactive";
        }
        
    }

    protected string GetSeparatorClassName(object item)
    {
        DataRowView row = (DataRowView)item;
        string currentUrl = Request.Url.ToString().ToLower();
        bool isActive = currentUrl.Contains(ScrubNodeUrl(row["Url"].ToString()));


        if (isActive)
        {
            return "activeSeparator";
        }
        else
        {
            return "inactiveSeparator";
        }

    }

    protected string GetTrailingImage(object item)
    {
        DataRowView row = (DataRowView)item;
        int index = Convert.ToInt32(row["Index"]);
        int totalIndexes = Convert.ToInt32(row["TotalIndexes"]);
        string currentUrl = Request.Url.ToString().ToLower();
        bool isActive = currentUrl.Contains(ScrubNodeUrl(row["Url"].ToString()));

		 
		
        if (index == (totalIndexes - 1) && isActive)
        {
            return "<td><img src=\"" + ResolveUrl( lastTabActiveRight) + "\" runat=\"Server\"></td>";
        }
        else if (index == (totalIndexes - 1) && !isActive)
        {
			return "<td><img src=\"" + ResolveUrl( lastTabInactiveRight) + "\" runat=\"Server\"></td>";
        }
        else
        {
            return "";
        }
    }

/*    protected override void Render(HtmlTextWriter writer)
    {
        writer.Write(@"<table width=""100%"" cellpadding=""0"" cellspacing=""0"" height=""20px"">");
        writer.Write(@"	<tr>");

        int i = 0;
        foreach (String node in nodes)
        {
            if (i == 0)
            {
                writer.Write(String.Format(
                    @"	    <td valign=""bottom""><img src=""/SqlWebAdminCS/App_Themes/default/images/tab{1}_home.gif"" /></td>" + Environment.NewLine +
                    @"      <td valign=""bottom"" height=""19px"" nowrap background=""/SqlWebAdminCS/App_Themes/default/images/tab{1}_bg.gif"" height=""19""/>{0}</td>"
                    , node, i == this.activeNode ? "on" : "off" ));
            }
            else if (i == nodes.Count - 1)
            {
                writer.Write(String.Format(
                    @"	    <td valign=""bottom""><img src=""/SqlWebAdminCS/App_Themes/default/images/tab{1}_between.gif"" /></td>" + Environment.NewLine +
                    @"      <td valign=""bottom"" height=""19px"" nowrap background=""/SqlWebAdminCS/App_Themes/default/images/tab{1}_bg.gif"" height=""19""/>{0}</td>"
                    , node, i == this.activeNode + 1 ? "on" : "off"));
                writer.Write(String.Format(@"	    <td valign=""bottom""><img src=""/SqlWebAdminCS/App_Themes/default/images/tab{0}_right.gif"" height=""19"" /></td>",
                    this.activeNode == this.nodes.Count - 1 ? "on" : "off"));
            }
            else
            {
                writer.Write(String.Format(
                    @"	    <td valign=""bottom""><img src=""/SqlWebAdminCS/App_Themes/default/images/tab{1}_{2}.gif"" /></td>" + Environment.NewLine +
                    @"      <td valign=""bottom"" height=""19px"" nowrap background=""/SqlWebAdminCS/App_Themes/default/images/tab{1}_bg.gif"" height=""19""/>{0}</td>"
                    , node, i == this.activeNode ? "on" : "off", i == this.activeNode ? "left" : "between" ));
            }
            i += 1;
        }
        //writer.Write(@"	    <td valign=""bottom""><img src=""/SqlWebAdminCS/App_Themes/default/images/taboff_home.gif"" /></td>");
        //writer.Write(@"	    <td valign=""bottom""><img src=""/SqlWebAdminCS/App_Themes/default/images/taboff_bg.gif"" width=""50"" height=""19""/></td>");
        //writer.Write(@"	    <td valign=""bottom""><img src=""/SqlWebAdminCS/App_Themes/default/images/taboff_between.gif"" /></td>");
        //writer.Write(@"	    <td valign=""bottom""><img src=""/SqlWebAdminCS/App_Themes/default/images/taboff_bg.gif"" width=""50"" height=""19""/></td>");
        //writer.Write(@"	    <td valign=""bottom""><img src=""/SqlWebAdminCS/App_Themes/default/images/tabon_left.gif"" height=""19"" /></td>");
        //writer.Write(@"	    <td valign=""bottom""><img src=""/SqlWebAdminCS/App_Themes/default/images/tabon_bg.gif"" height=""19"" width=""50"" /></td>");
        //writer.Write(@"	    <td valign=""bottom""><img src=""/SqlWebAdminCS/App_Themes/default/images/tabon_between.gif"" height=""19"" /></td>");
        //writer.Write(@"	    <td valign=""bottom""><img src=""/SqlWebAdminCS/App_Themes/default/images/taboff_bg.gif"" width=""50"" height=""19""/></td>");
        writer.Write(@"	    <td width=""100%"">&nbsp;</td>");
        writer.Write(@"	</tr>");
        writer.Write(@"</table>");
    }
*/
}
